package com.jk.service;

import com.jk.dao.*;
import com.jk.entity.RecordInvesEntity;
import com.jk.entity.BankCardEntity;
import com.jk.entity.BusinessEntity;
import com.jk.entity.InvestEntity;
import com.jk.entity.RechargeableEntity;
import com.jk.pojo.UserBean;
import com.jk.utils.Base64Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author 位佳生
 * @title: WithdrawService
 * @projectName springboot_financial
 * @description: TODO
 * @date 2022/11/716:40
 */
@Service
public class WithdrawService {
    @Autowired
    private BankCardDao bankCardDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private RechargeableDao rechargeableDao;
    @Autowired
    private InvestDao investDao;
    @Autowired
    private RecordInvesDao recordInvesDao;
    @Autowired
    private RedisTemplate redisTemplate;

    public String BankCardWithdraw(HttpSession session, BusinessEntity businessEntity) {
        UserBean userBean = (UserBean) redisTemplate.opsForValue().get("denglu");
        String code = (String) redisTemplate.opsForValue().get(userBean.getUser_phone());
        if (businessEntity.getT_code().equals(code)){
        if (userBean.getUser_with().equals(businessEntity.getT_pwd())){
            bankCardDao.saveBankCard(businessEntity);
            userDao.updateUser(businessEntity,userBean.getUser_id());
            BankCardEntity bankCardEntity = bankCardDao.selectBankById(businessEntity.getT_bank_code());
            RechargeableEntity rechargeableEntity = new RechargeableEntity();
            rechargeableEntity.setFinancialDetailsPaid(businessEntity.getT_money());
            rechargeableEntity.setFinancialDetailsBalance(userBean.getUser_balance().subtract(businessEntity.getT_money()));
            Date myDate = new Date();
            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
            String strDate1 = sdf1.format(myDate);
            rechargeableEntity.setFinancialDetailsDate(strDate1);
//                rechargeableEntity.setFinancialDetailsName(businessEntity.getT_bank_name());
            rechargeableEntity.setFinancialDetailsTypeId(4);
            rechargeableEntity.setFinancialDetailsName(bankCardEntity.getBank_card_name());
            rechargeableEntity.setFinancialUserPhone(userBean.getUser_phone());
            rechargeableDao.insert(rechargeableEntity);
            getByIdUserMoney();
            return "提现成功";
        }else {
            return "密码错误";
        }
        }else {
            return "验证码错误";
        }

    }

    public String getSave(Integer propertyId, BigDecimal price, String userPwd, String code) {

        UserBean userBean = (UserBean) redisTemplate.opsForValue().get("denglu");
        String code1 = (String) redisTemplate.opsForValue().get(userBean.getUser_phone());
        if (code.equals(code1) ){
            if (userPwd.equals(Base64Utils.decodeData(userBean.getUser_with()))){
                //修改用户余额
                userDao.updateUserById(userBean.getUser_id(),price);
                //修改可剩投资金额
                investDao.updateInvestById(propertyId,price);
                InvestEntity investEntity = investDao.queryInvestmentGetById(propertyId);
                RecordInvesEntity recordInvesEntity = new RecordInvesEntity();
                Date myDate = new Date();
                SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
                String strDate1 = sdf1.format(myDate);
                recordInvesEntity.setInvestment_date(strDate1);
                recordInvesEntity.setInvestment_money(price);
                recordInvesEntity.setInvestment_name(investEntity.getPropertyName());
                recordInvesEntity.setInvestment_rate(investEntity.getYearYield());
                recordInvesEntity.setInvestment_status(4);
                recordInvesEntity.setInvestment_type(investEntity.getPropertyType());
                recordInvesEntity.setInvestment_userid(userBean.getUser_id());
                recordInvesEntity.setInvestment_propertyid(propertyId);
                recordInvesEntity.setInvestment_userName(userBean.getUser_name());
                //新增用户投资记录
                recordInvesDao.insert(recordInvesEntity);
                //查询当前用户所有信息
                getByIdUserMoney();
                return "投资成功";
            }
            else {
                return "密码错误";
            }
        }else {
            return "验证码错误";
        }
    }
    /*
    *
     * @Author 位佳生
     * @Description //TODO 查询用户余额
     * @Date 2022/11/16 14:43
     
     * @return: com.jk.pojo.UserBean
     **/
    public void getByIdUserMoney() {
        UserBean userBean = (UserBean) redisTemplate.opsForValue().get("denglu");
        //查询当前用户所有信息
        UserBean userBean1 = userDao.selectByIdUser(userBean.getUser_id());
        redisTemplate.opsForValue().set("denglu",userBean1);
    }
}
